$(document).ready(function () {
    initPagination();
});


//条件查询开始
$('#putdatetime_condition').on('focus', function () {
    WdatePicker({
        onpicked: function () {
            console.log($(this).val());
        }
    })
})
$('#getdatetime_condition').on('focus', function () {
    WdatePicker({
        onpicked: function () {
            console.log($(this).val());
        }

    })
})


//条件查询开始
$("#distpicker").distpicker({
    autoSelect: false
});



//获取搜索条件
$('.seek-btn').on('click', function () {

    //发送ajax;
    query();
})
//清空条件
$('.seek-clear').on('click', function () {
    $('.form-control.uname').val('');
    $('.form-control.uphone').val('');
    $('.form-control.ctname').val('');
    $('.form-control.ptname').val('');
    $('.form-control.ptphone').val('');
    $('#putdatetime_condition').val('');
    $('.form-control.gtname').val('');
    $('.form-control.gtphone').val('');
    $('#getdatetime_condition').val('');
})

//条件查询结束
//表格部分开始

//表格头部升序 降序

$('.sequence').on('click', function () {
    if ($(this).hasClass('isup')) {
        $(this).removeClass('isup').addClass('isdown');
    } else if ($(this).hasClass('isdown')) {
        $(this).removeClass('isdown').addClass('isup');
    } else {
        $(this).siblings('.sequence').removeClass('isup').removeClass('isdown');
        $(this).addClass('isup');
    }

})

function edit_btn_show(obj) {
    var tr = $(obj).parents("tr");
    var aymoney = tr.find('.aymoney').text();//标的金额

    var aystat = tr.find(".aystat").text();//案源状态

    var ayrate = tr.find('.ayrate').text();//佣金比例

    var aypush = tr.find('.aypush').text();//佣金金额

    var ayextra = tr.find('.ayextra').text();//上报奖励

    var aygetpay = tr.find('.aygetpay').text();//接单奖励

    var isreturn = tr.find('.isreturn').text();//是否回款

    var hfrecord = tr.find('.hfrecord').text();//回访记录
    $('#aymoney').val(aymoney);
    $('#aystat').val(aystat);
    $('#ayrate').val(ayrate);
    $('#aypush').val(aypush);
    $('#ayextra').val(ayextra);
    $('#aygetpay').val(aygetpay);
    $('#isreturn').val(isreturn);
    $('#hfrecord').val(hfrecord);
    $('#id').val(tr.find('.id').text());
    $('#edit-info').show();
}

var pxzd = 'aycode';
var pxfx = 'desc';

var uwant = '';
var aystat = '';


function uwant_change(obj) {
    uwant = $(obj).val();
    initPagination();
}
function aystat_change(obj) {
    aystat = $(obj).val();
    initPagination();
}
function getJson(page_index, jq) {
    if (typeof(page_index) == "undefined") {
        page_index = 0;
    }
    var queryjson = {};
    if ($('.form-control.uname').val()) {
        queryjson.uname = $('.form-control.uname').val();

    }
    if ($('.form-control.uphone').val()) {
        queryjson.uphone = $('.form-control.uphone').val();

    }
    if ($('.form-control.ctname').val()) {
        queryjson.ctname = $('.form-control.ctname').val();

    }
    if ($('.form-control.ptname').val()) {
        queryjson.ptname = $('.form-control.ptname').val();

    }
    if ($('.form-control.ptphone').val()) {
        queryjson.ptphone = $('.form-control.ptphone').val();

    }
    if ($('#putdatetime_condition').val()) {
        queryjson.putdatetimestr = $('#putdatetime_condition').val();
    }
    if ($('.form-control.gtname').val()) {
        queryjson.gtname = $('.form-control.gtname').val();

    }
    if ($('.form-control.gtphone').val()) {
        queryjson.gtphone = $('.form-control.gtphone').val();

    }
    if ($('#getdatetime_condition').val()) {
        queryjson.getdatetimestr = $('#getdatetime_condition').val();
    }
    queryjson.pageNum = page_index + 1;
    queryjson.pxzd = pxzd;
    queryjson.pxfx = pxfx;
    queryjson.aystat=aystat;
    queryjson.uwant=uwant;
    console.log(queryjson);
    return queryjson;
}


function getCount() {
    var result = 0;
    $.ajax(
        {
            type: "post",
            url: "/source/count",
            data: getJson(0, 0),
            dataType: "json",
            async: false,
            success: function (data) {
                result = data.data;
            }, error: function () {
            alert("报错了");
        }
        }
    );
    return result;
}

//分页
function initPagination() {
    var totalcount = getCount();
    // 创建分页
    $("#Pagination").pagination(totalcount.total, {
        num_edge_entries: 0, //边缘页数
        num_display_entries: 8, //主体页数
        callback: pageselectCallback, //回调函数
        items_per_page: 10, //每页显示10项
        prev_text: "上一页",
        next_text: "下一页",
        ellipse_text: "...",//省略的文本
        prev_show_always: true,//是否总显示“前一页”分页按钮
        next_show_always: true,//是否总显示“下一页”分页按钮，
    });
    $("#currentCount").text("咨询数量："+totalcount.total);
}


//   表格部分结束
//全选按钮

$('#check-all').on('click', function () {
    $('.check-unit').prop('checked', $(this).prop('checked'))
})
//批量冻结弹框开始
$('.freeze').on('click', function () {
    var num = util.getCheck($('.check-unit')).length;
    if (num == 0) {
        alert('请先选择要冻结的用户');
        return;
    }
    $('.freeze-quesition').html('确定要将选中的' + num + '个用户冻结吗?');
    $('.freeze-info').html('冻结后客户将不能实现咨询、关注律师、更换专属律师 、充值等行为。');
    $('#freeze-info').cmsmodal('show');

    //确定
    $('.freeze-confirm').off('click').on('click', function () {
//ajax
        var arrayObj = new Array();
        var rows = $('.check-unit');
        for (x in rows) {
            if (rows[x].checked == true) {
                var operate = $("#table1 tbody tr:eq(" + x + ") ").find("td.operate");
                var editbtn = $(operate).find(".edit-btn");
                arrayObj.push($(editbtn).attr("data-value"));
            }
        }
        var json = {list: arrayObj, freezeType: 'freeze'};
        $.ajax(
            {
                type: "post",
                url: "/source/batchFreeze",
                data: json,
                dataType: "json",
                success: function (data) {
                    initPagination()
                }, error: function () {
                alert("报错了");
            }
            }
        );

        $('#freeze-info').cmsmodal('hide');
    })
    //取消
    $('.freeze-cancel').off('click').on('click', function () {
        $('#freeze-info').cmsmodal('hide');
    })
})

//批量冻结弹框结束

//批量解冻弹框开始
$('.unfreeze').on('click', function () {
    var num = util.getCheck($('.check-unit')).length;
    if (num == 0) {
        alert('请先选择要解冻的案源');
        return;
    }
    $('.freeze-quesition').html('确定要将选中的' + num + '个案源解冻吗?');
    $('.freeze-info').html('解冻后客户将恢复咨询、关注律师、更换专属律师、充 值等功能。');
    $('#freeze-info').cmsmodal('show');

    //确定
    $('.freeze-confirm').off('click').on('click', function () {
        //ajax
        var arrayObj = new Array();
        var rows = $('.check-unit');
        for (x in rows) {
            if (rows[x].checked == true) {
                var operate = $("#table1 tbody tr:eq(" + x + ") ").find("td.operate");
                var editbtn = $(operate).find(".edit-btn");
                arrayObj.push($(editbtn).attr("data-value"));
            }
        }
        var json = {list: arrayObj, freezeType: 'unFreeze'};
        $.ajax(
            {
                type: "post",
                url: "/source/batchFreeze",
                data: json,
                dataType: "json",
                success: function (data) {
                    initPagination()
                }, error: function () {
                alert("报错了");
            }
            }
        );

        $('#freeze-info').cmsmodal('hide');
    })
    //取消
    $('.freeze-cancel').off('click').on('click', function () {
        $('#freeze-info').cmsmodal('hide');
    })
})

//批量解冻弹框结束


// 超出部分  ...
$('.p').ellipsis({maxWidth: 140, maxLine: 2});
 //function showEllipsis(obj) {


 function showDetail(obj) {
    if ($('.detail-text-box').length > 0) {
        $('.detail-text-box').remove();
    }else{
        var  txt=obj.innerText;
        var str =
            '<div class="detail-text-box">' +
            '<div class="detail-box-close">x</div>' +
            '<div class="detail-text-content"> ' + txt + '</div>' +
            '</div>';
        $(str).appendTo($(obj));
        $('.detail-text-box').show();
        if ($('.table-top').get(0).scrollHeight - $('.table-top').height() > 25) {
            $('.detail-text-box').css({
                'bottom': 0
            })
        } else {
            $('.detail-text-box').css({
                'top': 0
            })
        }
    }

}

function parseAyarea(obj){
     //var  pcd=obj;
    //var  pcdstr =pcd.replace(new RegExp('"',"gm"),'');
    var  ayarea='';
    if(obj){
        if(obj.indexOf("{")!=-1){
            var pcdobj=JSON.parse(obj);
            if(pcdobj.p){
                if(pcdobj.p.text != '全部'){
                    ayarea= ayarea+pcdobj.p.text;
                    ayarea=ayarea+" ";
                }
            } if(pcdobj.c){
                if(pcdobj.c.text != '全部'){
                    ayarea= ayarea+pcdobj.c.text;
                    ayarea=ayarea+" ";
                }
            } if(pcdobj.d){
                if(pcdobj.d.text != '全部'){
                    ayarea= ayarea+pcdobj.d.text;
                }
            }
        }else{
            ayarea=obj;
        }
    }
    return ayarea;

  //   console.log(obj);
}


$('td.aytxt.two-row').on('click', '.detail-box-close', function () {
    $(this).parent().remove();
    return false
})

//   编辑弹框确定
function editConfirm() {


    var json_update = {};
    json_update.id = $('#id').val();
    json_update.aymoney = $('#aymoney').val()==''?0:accMul($('#aymoney').val(),100);
    json_update.aystat = $('#aystat').val();
    json_update.ayrate = $('#ayrate').val();
    json_update.aypush = $('#aypush').val()==''?0:accMul($('#aypush').val(),100);
    json_update.ayextra = $('#ayextra').val()==''?0:accMul($('#ayextra').val(),100);
    json_update.aygetpay = $('#aygetpay').val()==''?0:accMul($('#aygetpay').val(),100);
    json_update.isreturn = $('#isreturn').val();
    json_update.hfrecord = $('#hfrecord').val();
    console.log(json_update);
    $.ajax(
        {
            type: "post",
            url: "/source/updateAnyuan",
            data: json_update,
            dataType: "json",
            success: function (data) {
                alert("修改成功！");
            }, error: function () {
            alert("报错了");
        }
        }
    );
    cleanEditInfo();
    $('#edit-info').hide();
    initPagination();
}

$('.edit-info-cancel').on('click', function () {
    cleanEditInfo();
    $('#edit-info').hide()
})

function cleanEditInfo() {
    $('#edit-info').hide();
    $('#aymoney').val('');
    $('#aystat').val('');
    $('#ayrate').val('');
    $('#aypush').val('');
    $('#ayextra').val('');
    $('#aygetpay').val('');
    $('#isreturn').val('');
    $('#hfrecord').val('');
    $('#id').val('');
}
function pageselectCallback(page_index, jq) {
    query(page_index, jq);
}
//查询
function query(page_index, jq) {
    $.ajax(
        {
            type: "post",
            url: "/source/queryAnyuan",
            data: getJson(page_index, jq),
            dataType: "json",
            success: function (data) {
                var result = data.data;
                var tmpl1 = bt('list1', result);
                $("#tbody_anyuan").html(tmpl1);
            }, error: function () {
            alert("报错了");
        }
        }
    );
}

function accMul(arg1, arg2) {

    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();

    try { m += s1.split(".")[1].length } catch (e) { }

    try { m += s2.split(".")[1].length } catch (e) { }

    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)

}


function exportxls() {
    var json = getJson();
    location.href="/source/exportExcel?uname="+json.uname+"&uphone="+json.uphone+"&uphoneArea="+json.uphoneArea
        +"&lawyername="+json.lawyername+"&alawyername="+json.alawyername+"&firstTimeStart="+json.firstTimeStart+"&lastTimeStart="+json.lastTimeStart
        +"&lastTimeEnd="+json.lastTimeEnd+"&createTimeStart="+json.createTimeStart+"&createTimeEnd="+json.createTimeEnd+"&lawyerPhone="+json.lawyerPhone
        +"&lawyerStates="+json.lawyerStates+"&initiator="+json.initiator+"&callStatus="+json.callStatus+"&startTimeStart="+json.startTimeStart
        +"&startTimeEnd="+json.startTimeEnd+"&calleffect="+json.calleffect+"&acid="+json.acid;
}